package com.health.home.controller;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.health.home.domain.po.CarouselimagePo;
import com.health.home.service.CarouselimageService;
import com.health.home.util.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author xpz
 * @date 2024/12/15 14:34
 */
@RestController
@RequestMapping("/image")
public class CarouselimageController {

    @Autowired
    private CarouselimageService carouselimageService;

    @Resource
    private FastFileStorageClient storageClient;


    /**
     * 轮番图后台管理
     */
    @GetMapping("/CarouselimageList")
    public Result<List<CarouselimagePo>> CarouselimageList(){
        List<CarouselimagePo> list = carouselimageService.list();
        return Result.success(list);
    }


    /**
     * 新增轮番图
     */
    @PostMapping("/addCarouselimage")
    public Result<String> addCarouselimage(@ModelAttribute CarouselimagePo carouselimagePo){
        carouselimageService.save(carouselimagePo);
        return Result.success("新增轮番图成功");
    }
    /**
     * 删除轮番图
     */
    @PostMapping("/deleteCarouselimage")
    public Result<String> deleteCarouselimage(Integer id){
        carouselimageService.removeById(id);
        return Result.success("删除轮番图成功");
    }
    /**
     * 修改轮番图
     */
    @PostMapping("/updateCarouselimage")
    public Result<String> updateCarouselimage(@ModelAttribute CarouselimagePo carouselimagePo){
        carouselimageService.updateById(carouselimagePo);
        return Result.success("修改轮番图成功");
    }

    /**
     * 多图片上传 trackerLocator
     */
    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file){
        StorePath storePath=null;
        String s = file.getOriginalFilename().split("\\.")[1];
        try {
            storePath=storageClient.uploadFile(file.getInputStream(),file.getSize(),s,null);
        } catch (IOException e) {

            throw new RuntimeException(e);
        }
        String fullPath = storePath.getFullPath();
        return Result.success("http://124.223.70.108:8888/"+fullPath);
    }


}
